This frame contains the script that is responsible for doing the 3D calculations, drawing the vertices and connecting them with lines.
The script on this frame is also called from the main looping script and is responsible for doing the actual 3Dcalculations and converting the 3D coordinates (X, Y, Z) to 2d X, Y) coordinates so that the points can be drawn on screen. It is important to understand that any 3D environment on a computer screen is an illusion of 3D space. A screen is essentially a 2D surface and the trick is to arrive at X and Y coordinates that can be used to draw the pointΓÇÖs position on screen, and a Z coordinate that can be used to scale the points in order to create a sense of depth.
The script uses the "x", "y" and "z" coordinates for each point (set in frame 2) and the rotation angles (Variable: "Yangle" and "Xangle" to arrive at values for the X, Y and Z positions.
The calculations for rotation around all three axis will look something like the equation below and requires a value for the X rotation, Y rotation and Z rotation and values for "x", "y" and "z". The calculation is done nine times (once for each vertex with the value of Variable: "c" corresponding to the vertex number). This is where the Sine and Cosine values (set in frame 1) comes into play.
Note that the below equations contains the formula for calculating the X and Y coordinates on a circle on a single plane. I covered this earlier ( remember: radius * cos (angle) for X 0and radius * sin (angle) for Y.The reason for this is that we are basicly calculating a points position on a circular path, not only for one plane, but for three (one for each axis) with "x","y" and "z" representing the radius or the distance away from the center of rotation on each axis (the coordinates set in frame 2):
Rotation around X-axis:
ypos = y * cos (Xangle) - z * sin(Xangle)
zpos = y * sin(Xangle) + z * cos(Xangle)
Rotation around Y-axis
xpos = x * cos(Yangle) + zpos * sin(Yangle)
zpos = -x *sin(Yangle) + zpos * cos(Yangle)
Rotation around Z-axis:
xpos = xpos * cos(Zangle) ΓÇö ypos * sin(Zangle)
ypos = xpos * sin(Zangle) + ypos* cos(Zangle)
Because the X and Y position of "/TrailerObj" was used to generate the rotation angles, there is no value set for the rotation around the Z-axis. You can therefore take a bit of a shortcut and only calculate the rotation around the X and Y-axis. YouΓÇÖll see the difference between the two methods in the way the 3D object reacts when rotated.
Calculating the rotation for X, Y and Z as IΓÇÖve shown you would be a more thorough way to do it, but since I was quite happy to lose two lines of calculations, I missed out Z and did it this way:
Rotation around Y-axis
zpos = z * cos(Yangle) ΓÇö x * sin(Yangle)
xpos = z * sin"(Yangle) + x * cos(Yangle)
Rotation around X-axis
ypos = y * cos(Xangle) - zpos * sin(Xangle)
zpos = y * sin(Xangle) + zpos * cos(Xangle)
Now that the individual vertices are drawn and scaled according to their depth, itΓÇÖs time to give these floating points some structure by connecting them with lines. Because it is not possible to plot individual vertex coordinates for a line in Flash we have to find another way round. This can be done by using a movie clip that contains a single diagonal hairline. The idea is to duplicate "/line" and to put the new instance at the same coordinate as the first point and then scaling it to stretch across to the next point. Because the line is a 100 units high and wide its scale percentage and actual size has the same value, in other words, if we set its Y scale to 50% of its original height of 100 units, it would scale down to 50 units etc. Note that that the movie clip wonΓÇÖt scale a negative value unless it is duplicated. (Scaling a movie clip that is not a duplicate with a negative value can yield some unexpected results.)